/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderLog;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderLogService;

/**
 * 订单明细Controller
 * @author sxh
 * @version 2018-08-15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuOrderLog")
public class JiuOrderLogController extends BaseController {

	@Autowired
	private JiuOrderLogService jiuOrderLogService;
	
	@ModelAttribute
	public JiuOrderLog get(@RequestParam(required=false) Long id) {
		if (id != null){
			return jiuOrderLogService.get(id);
		}else{
			return new JiuOrderLog();
		}
	}
	
	@RequiresPermissions("jiujiudevice:jiuOrderLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuOrderLog jiuOrderLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		/*if (!user.isAdmin()){
			jiuOrderLog.setUser(user);
		}*/
        Page<JiuOrderLog> page = jiuOrderLogService.find(new Page<JiuOrderLog>(request, response), jiuOrderLog); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuOrderLogList";
	}

	@RequiresPermissions("jiujiudevice:jiuOrderLog:view")
	@RequestMapping(value = "form")
	public String form(JiuOrderLog jiuOrderLog, Model model) {
	
		User user = UserUtils.getUser();
		/*if (jiuOrderLog.getUser() == null || jiuOrderLog.getUser().getId() <1){
			jiuOrderLog.setUser(user);
		}*/
		
		model.addAttribute("jiuOrderLog", jiuOrderLog);
		return "modules/jiujiudevice/jiuOrderLogForm";
	}

	@RequiresPermissions("jiujiudevice:jiuOrderLog:edit")
	@RequestMapping(value = "save")
	public String save(JiuOrderLog jiuOrderLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuOrderLog)){
			return form(jiuOrderLog, model);
		}
		jiuOrderLogService.save(jiuOrderLog);
		addMessage(redirectAttributes, "保存订单明细'" + jiuOrderLog.getId() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuOrderLog/?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuOrderLog:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuOrderLogService.delete(id);
		addMessage(redirectAttributes, "删除订单明细成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuOrderLog/?repage";
	}

}
